import { isNull, isNumber } from '@/utils/is'
import { UserGroup, QuestionGroup } from './type'
import { UserDataItem } from '@/api/mark/type'

export const tabsList = [
	{
		label: '按人批阅',
		value: 'user'
	},
	{
		label: '按题批阅',
		value: 'question'
	}
]

export const getProcessInfo = (all: number, unMark: number) => {
	if (unMark === 0)
		return {
			text: '已批阅',
			color: '#26A555'
		}
	if (unMark === all) return { text: '未批阅', color: '#79828F' }
	return {
		text: `进度:${all - unMark}/${all}`,
		color: '#FFA126'
	}
}

export const getScoreInfo = (totalScore:number, score: number | null) => {
  console.log("score",isNumber(score))
	if (!isNumber(score)) return { text: '未批阅', color: '#79828F' }
	// if(result === 0)return {text:'0分',color:'#FF4C26'}
	if (score === 0) return { text: `${score}分`, color: '#FF4C26' }
	//未满分
	if (score !== totalScore) return { text: `${score}分`, color: '#FFA126' }
	//满分
	return {
		text: `${score}分`,
		color: '#26A555'
	}
}

export const getScoreInfoByTotal = (total: number, score: number | null) => {
	const res = {
		text: `${score}`,
		endText: '分',
		color: '#26A555'
	}
	if (!isNumber(score)) {
		res.text = '—'
		res.endText = ''
		res.color = '#C9CED6'
	} else if (score === 0) {
		res.color = '#FF4C26'
	} else if (score < total) {
		res.color = '#FFA126'
	}
	return res
}
export const clacUserProcessInfo = (data: UserGroup) => {
	const allNum = data.childList.length
	const unMarkNum = data.childList.reduce((pre, cur) => {
		return  pre + (isNumber(cur.score)? 0:1)
	}, 0)
	return getProcessInfo(allNum, unMarkNum);
}
export function transformToUserGroup(data: UserDataItem[]): UserGroup[] {
  if (!data) return [] ;
	return data.map((item) => {
		return {
			name: item.name,
			id: item.uid,
			imgUrl: item.imgUrl,
			processInfo: getProcessInfo(item.allNum, item.unMarkNum),
			childList: item.markItem.map((item) => {
				return {
					id: item.questionId,
					numStr: item?.questionNum ?? `${item.num}.`,
          numText:item?.questionNum ?? item.num,
					scoreInfo: getScoreInfo(item.questionScore, item.myscore),
					name: item.answerItem.questionCode.name ?? '',
					cannot: item.answerItem.cannot,
					parentQuestionId: item.parentQuestionId,
					totalScore: item.questionScore,
					topicId: item.topicId,
					splitTaskId: item.splitTaskId,
					num: item.num,
					score: item.myscore,
					label: item.markTopicId?.[0] ?? '',
					answerList: item.answerItem.value ?? [],
					markInfo: item.markInfo ?? []
				}
			})
		}
	})
}
/**
 * 将 user 分组数据转换成 question 分组数据
 */
export function transformUserToQuestionGroup(
  userList: UserGroup[]
): QuestionGroup[] {
  const questionMap = new Map<string, QuestionGroup>()

  for (const user of userList) {
    for (const item of user.childList) {
      if (!item.id) continue

      let question = questionMap.get(item.id)
      if (!question) {
        question = {
          id: item.id,
          numStr: item.numStr,
          name: item.name,
          processInfo: {
            text: '',
            color: ''
          },
          childList: []
        }
        questionMap.set(item.id, question)
      }

      // 构建子项并填充 scoreInfo
      question.childList.push({
        id: user.id,
        name: user.name,
        imgUrl: user.imgUrl,
        cannot: item.cannot,
        scoreInfo: item.scoreInfo,
      })
    }
  }

  // 填充 processInfo（基于每个问题的 childList 中 myScore 是否为 null）
  for (const [_, question] of questionMap) {
    const total = question.childList.length
    const markedCount = question.childList.filter(
      (user) => user.scoreInfo.text !== '未批阅'
    ).length

    question.processInfo = getProcessInfo(total, total - markedCount)
  }

  return Array.from(questionMap.values())
}
